﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GSite.Model.Entities;
using GSite.Model.UnitOfWork;

namespace GSite.Model.Repositories
{
    public class UserGroupRepository : RepositoryBase, IUserGroupRepository
    {
        #region Public 

        public UserGroupRepository(IUnitOfWork unitOfWork)
        {
            UnitOfWork = unitOfWork;
        }

        public UserGroup GetById(Guid id)
        {
            return DataContext.UserGroups.Single(u => u.Id == id);
        }

        public IEnumerable<UserGroup> GetAll()
        {
            return DataContext.UserGroups;
        }

        public Dictionary<string, Guid> GetSpecific(params string[] ugNames)
        {
            if (ugNames.Length == 0)
                return null;

            return DataContext.UserGroups.Where(e => ugNames.Contains(e.Name)).AsEnumerable().ToDictionary(e => e.Name, e => e.Id);                    
        }

        public string GetNameById(Guid? id)
        {
            if (!id.HasValue)
                return null;

            var userGroup = DataContext.UserGroups.FirstOrDefault(e => e.Id == id);

            return userGroup == null ? null : userGroup.Name;
        }

        #endregion
    }
}
